﻿$(document).ready(function () {
    
    $('#submitForm').submit(function () {
        $(this).serialize();
        return true;
    });

    /*
    var byName = $.getUrlVar('photoPath');
    if (byName != null || byName != '') {
        rotozoom(byName);
    }

    /*
    var byName1 = $.getUrlVar('photoPath1');
    if (byName1 != null || byName1 != '') {
        rotozoom(byName1);
    }
    */

    var photos = $.getUrlVars();
    //alert($.getUrlVars());

    //no get param
    if (window.location.href == photos[0]) {        
        //$('.zoomProps').attr('style', 'display:true');
        refreshDiv();
        return;
    }
    for (var i = 0; i < photos.length; i++) {
        if (i == 3) {
            break;
        }
        var uri = $.getUrlVars()[photos[i]];
        rotozoom(decodeURIComponent(uri), i);
    }
    
});


$.extend({
    getUrlVars: function () {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function (name) {
        return $.getUrlVars()[name];
    }
});


function rotozoom(photoPath,id) {       

    //$.ajaxSetup({ cache: false });
   // var photoPath = $('#pathPhoto').val();
    if (!photoPath) {
        //alert('EMPTY');
        return;
    }
    var req = photoPath;

    //alert(req);


    $.ajax({
        url: req,
        type: 'HEAD',
        error: function () {
            //file not exists
            alert('file does not exist : ' + photoPath); 
        },
        success: function () {
            //file exists
            //alert('file exist');


            //('#photo').attr('src', '/images/' + photoPath);

            prepareBaliseImg(req, id);

            //$('<img/>').attr('src', req).appendTo($('#polaroid'));

            refreshDiv();            
        }
    });          
    
}


function prepareBaliseImg(photoPath, id) {

    console.log(photoPath);

    $('#photo'+id).attr('src', photoPath);

   // $("#zoomwrapper").append('<div id="zoom' +  id + '" class="zoomProps"> <div class="polaroid"> <img id="photo' + id + '" alt="waiting" width="400" height="400" src="' + photoPath + '" />  </div> </div> ');


}

function refreshDiv() {       
    $("#zoomwrapper").load(); 
}
